﻿
// ================================================================================
//        File: frmStockIn.cs
//        Desc: 入库操作
//        Auth: JF
//        Date: 2010-09-23
// ================================================================================
//        Change History
// ================================================================================
//        Date:         Author:                  Description:
//        --------     --------              -------------------
//         10/18       JF                      1，增加入库提示，在入库前进行一次确认是否录入正确
//                                             2，优化输入方式，每在一个输入框中按回车键，光标将自动跳入到下一个输入框内，并给出相应提示
//                                             3，修正MessageBox提示为Label提示，增加界面友好度。
//          11/1        JF                      1, 修改入库总价为自动运算
//                                              2, 修正失效日期小于当前日期的BUG，失效日期必须大于1天
//
// ================================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmStockIn : frmBase
    {

    //    M.Barcode,M.MerchandiseClassID,C.MerchandiseClassName,M.MerchandiseName,M.MerchandiseUnitID, U.UnitName, M.MerchandiseResume, M.MerchandiseRemark,
    //  M.IsAlert,M.AlertNum, M.HasOverTime
        public frmStockIn()
        {
            InitializeComponent();
            Reset();
        }
       
        private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            lblMsg.Visible = false;
            if (string.IsNullOrEmpty(this.txtBarcode.Text)) return;//if the barcode is empty then return
            if (e.KeyChar == (char)13)// if the key code is 13 then operation.
            {
                MerchandiseBasicInfo oMerchandiseBasicInfo = new StockInBLL().GetMerchandiseInfo(txtBarcode.Text.Trim());
                if (oMerchandiseBasicInfo == null)
                {
                    //Open add merchandise window
                    lblMsg.Visible = true;
                    lblMsg.Text = "商品信息表中没有条码为 " + txtBarcode.Text.Trim() + " 的商品，如有需要，请联络管理员添加。";
                    this.txtBarcode.SelectAll();
                    this.txtBarcode.Focus();
                }
                else
                {
                    this.txtMerchandiseClassName.Text = oMerchandiseBasicInfo.MerchandiseClassName;
                    this.txtMerchandiseName.Text = oMerchandiseBasicInfo.MerchandiseName;
                    this.txtAlterNum.Text = oMerchandiseBasicInfo.AlertNum.ToString();
                    this.txtMerchandiseUnitName.Text = oMerchandiseBasicInfo.MerchandiseUnitName;
                    this.rtbMerchandiseResume.Text = oMerchandiseBasicInfo.MerchandiseResume;
                    this.rtbMerchandiseRemark.Text = oMerchandiseBasicInfo.MerchandiseRemark;
                    this.cbxIsAlter.Checked = oMerchandiseBasicInfo.IsAlert;
                    this.cbxIsOverTime.Checked = oMerchandiseBasicInfo.HasOverTime;
                    btnSubmit_Click(sender, e);
                }

            }
        }

        //Submit
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            lblMsg.Visible=true;
            if (string.IsNullOrEmpty(txtBarcode.Text))
            {
                //MessageBox.Show("请扫入商品条码");
                lblMsg.Text = "请扫入商品条码";
                this.txtBarcode.Focus();
                this.txtBarcode.SelectAll();
                return;
            }
            if (string.IsNullOrEmpty(txtMerchandiseName.Text))
            {
                //MessageBox.Show("目标商品条码不存在");
                lblMsg.Text = "目标商品条码不存在";
                this.txtBarcode.Focus();
                this.txtBarcode.SelectAll();
                return;
            }
            if (string.IsNullOrEmpty(txtStockInNum.Text))
            {
                lblMsg.Text = "请输入入库数量";
                this.txtStockInNum.Focus();
                this.txtStockInNum.SelectAll();
                return;
            }
            if (string.IsNullOrEmpty(txtUnitPrice.Text))
            {
                lblMsg.Text = "请输入进货单价";
                this.txtUnitPrice.Focus();
                this.txtUnitPrice.SelectAll();
                return;
            }
            if (string.IsNullOrEmpty(txtTotalPrice.Text))
            {
                lblMsg.Text = "请输入进货总价";
                this.txtTotalPrice.Focus();
                this.txtTotalPrice.SelectAll();
                return;
            }
            if (Convert.ToInt32(txtStockInNum.Text) < 1)
            {
                //MessageBox.Show("入库的商品数量必须大于1");
                lblMsg.Text = "请输入有效的入库数量";
                this.txtStockInNum.Focus();
                this.txtStockInNum.SelectAll();
                return;
            }

            if (Convert.ToDouble(txtUnitPrice.Text) <= 0)
            {
                //MessageBox.Show("请输入商品的价格，且商品价格不能为0");
                lblMsg.Text = "请输入有效的进货单价";
                this.txtUnitPrice.Focus();
                this.txtUnitPrice.SelectAll();
                return;
            }

            if (Convert.ToDouble(txtTotalPrice.Text) == 0)
            {
                //MessageBox.Show("请输入商品的总价格，且商品总价格不能为0");
                lblMsg.Text = "请输入有效的进货总价";
                this.txtTotalPrice.Focus();
                this.txtTotalPrice.SelectAll();
                return;
            }
            if (cbxIsOverTime.Checked && Convert.ToDateTime(dtpOverTime.Text) < DateTime.Now.AddDays(1))
            {
                //MessageBox.Show("请选择商品逾期日期，且日期必须是大于当前时间。");
                lblMsg.Text = "请输入有效的逾期日期";
                this.dtpOverTime.Focus();
                return;
            }
            lblMsg.Visible=false;

            StockIn oStockIn = new StockIn();
            oStockIn.Barcode = SystemLibrary.SQLQuoteName(txtBarcode.Text.Trim());
            oStockIn.StockInNum = Convert.ToInt32(txtStockInNum.Text);
            oStockIn.UnitPrice = Convert.ToDouble(txtUnitPrice.Text.Trim());
            oStockIn.TotalPrice = Convert.ToDouble(txtTotalPrice.Text.Trim());
            oStockIn.OverTime = dtpOverTime.Value;
            StockInBLL oStockInBLL = new StockInBLL();
            string strMsg = "您正在执行入库操作，入库完成后将无法对库存数量进行再次修改，您录入的信息如下：\t\t";
            strMsg += "\r\n\t商品条码：" + oStockIn.Barcode;
            strMsg += "\r\n\t商品名称：" + txtMerchandiseName.Text;
            strMsg += "\r\n\t入库数量：" + oStockIn.StockInNum.ToString();
            strMsg += "\r\n\t进货单价：" + Common.FormatManager.FormatDecimal(Convert.ToDecimal(oStockIn.UnitPrice));
            strMsg += "\r\n\t进货总价：" + Common.FormatManager.FormatDecimal(Convert.ToDecimal(oStockIn.TotalPrice));
            if (cbxIsOverTime.Checked)
                strMsg += "\r\n\t逾期时间：" + oStockIn.OverTime.ToString("yyyy-MM-dd");
            strMsg += "\r\n\r\n请确认录入的数据是否正确？";

            if (MessageBox.Show(strMsg, "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                if (oStockInBLL.AppendStockIn(oStockIn))
                {
                    MessageBox.Show("入库成功");
                    Reset();
                }
                else
                {
                    MessageBox.Show("入库失败");
                }
            }
            
        }

        private void cbxIsOverTime_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbxIsOverTime.Checked)
            {
                this.dtpOverTime.Enabled = true;
            }
            else
            {
                this.dtpOverTime.Enabled = false;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            foreach (Control control in this.Controls)
            {
                if (control is TextBox) { ((TextBox)control).Clear(); continue; }
                if (control is RichTextBox) { ((RichTextBox)control).Clear(); continue; }
            }
            cbxIsAlter.Checked = false;
            cbxIsOverTime.Checked = false;
            dtpOverTime.Value = DateTime.Now;
            this.txtAlterNum.Text = "0";
            this.txtStockInNum.Text = "0";
            this.txtUnitPrice.Text = "0.00";
            this.txtTotalPrice.Text = "0.00";
            this.txtBarcode.SelectAll();
            this.txtBarcode.Focus();
        }

        #region Verify Input

        private void txtStockInNum_TextChanged(object sender, EventArgs e)
        {
            string strTmp = this.txtStockInNum.Text.Trim();
            try
            {
                int i = Convert.ToInt32(txtStockInNum.Text);
            }
            catch
            {
                this.txtStockInNum.Text = strTmp;
            }
        }

        private void txtUnitPrice_TextChanged(object sender, EventArgs e)
        {
            string strTmp = this.txtUnitPrice.Text.Trim();
            try
            {
                double d = Convert.ToDouble(txtUnitPrice.Text);
            }
            catch
            {
                this.txtUnitPrice.Text = strTmp;
            }
        }

        private void txtTotalPrice_TextChanged(object sender, EventArgs e)
        {
            string strTmp = this.txtTotalPrice.Text.Trim();
            try
            {
                double d = Convert.ToDouble(txtTotalPrice.Text);
            }
            catch
            {
                this.txtTotalPrice.Text = strTmp;
            }
        }

        private void txtAlterNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = Convert.ToInt32(this.txtAlterNum.Text);
            }
            catch
            {
                this.txtAlterNum.Text = "0";
            }
        }

        #endregion

        private void txtStockInNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnSubmit_Click(sender, new EventArgs());
            }
        }

        private void txtUnitPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                btnSubmit_Click(sender, new EventArgs());
        }

        private void txtTotalPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                btnSubmit_Click(sender, new EventArgs());
        }

        private void dtpOverTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==(char)13)
                btnSubmit_Click(sender,new EventArgs());
        }

        private void txtStockInNum_Leave(object sender, EventArgs e)
        {
            try
            {
                decimal mStockInNum = Convert.ToDecimal(txtStockInNum.Text);
                decimal mUnitPrice = Convert.ToDecimal(txtUnitPrice.Text);
                decimal mTotalPrice = mUnitPrice * mStockInNum;
                this.txtTotalPrice.Text = Common.FormatManager.FormatDecimal(mTotalPrice);
            }
            catch { }
        }

        private void txtUnitPrice_Leave(object sender, EventArgs e)
        {
            decimal mStockInNum = Convert.ToDecimal(txtStockInNum.Text);
            decimal mUnitPrice = Convert.ToDecimal(txtUnitPrice.Text);
            decimal mTotalPrice = mUnitPrice * mStockInNum;
            this.txtTotalPrice.Text = Common.FormatManager.FormatDecimal(mTotalPrice);
        }


    }
}
